import { NextRequest, NextResponse } from 'next/server';
import { getDb } from '@/lib/json-database';

export async function GET(request: NextRequest) {
  try {
    const db = getDb();
    const devices = db.devices;

    // 按分类码统计设备数量
    const counts: Record<string, number> = {};
    devices.forEach(device => {
      const classificationCode = device.classificationCode || 'unknown';
      counts[classificationCode] = (counts[classificationCode] || 0) + 1;
    });

    return NextResponse.json({
      success: true,
      data: counts
    });
  } catch (error) {
    const message = error instanceof Error ? error.message : 'An unknown error occurred';
    console.error(`Failed to get device counts: ${message}`);
    return NextResponse.json({
      success: false,
      error: message
    }, { status: 500 });
  }
}